﻿(function () {
    
    "use strict";
    
    //Debug Variables
    var DEBUG = false;

    function log(str) {
        
        if (DEBUG && console && console.log) {
            console.log(str);
        }
    };

   
    function updateTile() {

        //array containing the ids of the secondary tiles to be updated
        var tilesToBeUpdated = [];
        var posts = [];
        //get all categories and check wheather they are pinned
        Data.getCategories().then(function(parameters) {
            log("SecondaryTile.");
            if (parameters !== null) {
                       
                for (var i = 0; i < parameters.length; i++) {
                    log(parameters[i].title);
                    TilesManager.IsPinned("SecondaryTile." + parameters[i].id).then(function(pinned) {
                        log("ispinned: " + pinned);
                        if (pinned === true) {
                            tilesToBeUpdated.push(parameters[i]);
                        }
                    });
                }
            }

        }).then(function(parameters) {
            log(tilesToBeUpdated.length);
            var promises = [];
            var saveIds = [];
            for (var i = 0; i < tilesToBeUpdated.length; i++) {
                var catItem = tilesToBeUpdated[i];
                log("catid: " + catItem.id);
                saveIds[i] = catItem.id;
                promises[i] = Data.getPostsByCategoryCatItem(catItem, 5, false);
            }
            return WinJS.Promise.join(promises).then(function(e) {
                //write category id into each post
                for (var j = 0; j < e.length; j++) {
                    if (typeof e[j] !== "undefined" && e[j] !== null) {
                        for (var k = 0; k < e[j].length; k++) {
                            e[j][k].catId = saveIds[j];
                        }
                    }
                }
                return e;
            });
        }).then(function(result) {

            log("result" + result.length);
            for (var i = 0; i < result.length; i++) {
                for (var j = 0; j < result[i].length; j++) {
                    var post = result[i][j];

                    var wideTileContent = null;

                    if (typeof post.teaserImage !== "undefined" && post.teaserImage !== false) {
                        //create tile with image
                        wideTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileWideSmallImageAndText04();
                        wideTileContent.textHeading.text = post.title,true;
                        wideTileContent.textBodyWrap.text = post.teaserContent,true;
                        wideTileContent.image.src = post.teaserImage;

                        wideTileContent.branding = 2;
                        
                        
                        var squareTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileSquareText04();
                        squareTileContent.textBodyWrap.text = post.title,true;

                        wideTileContent.squareContent = squareTileContent;

                    } else {
                        //create tile without image
                        wideTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileWideBlockAndText02();
                        wideTileContent.textBodyWrap.text = post.title,true;

                        wideTileContent.branding = 2;

                        var squareTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileSquareText04();
                        squareTileContent.textBodyWrap.text = post.title,true;

                        wideTileContent.squareContent = squareTileContent;
                    }

                    

                    var secondaryTileID = "SecondaryTile." + post.catId;
                    try {
                        var tileUpdateManager = Windows.UI.Notifications.TileUpdateManager.createTileUpdaterForSecondaryTile(secondaryTileID);
                        tileUpdateManager.update(wideTileContent.createNotification());
                        tileUpdateManager.enableNotificationQueue(true);
                    } catch(e) {

                    } 
                }
            }
        });
        
        Data.getRecentPosts(5,false).then(function (posts) {
            var tileUpdateManager = Windows.UI.Notifications.TileUpdateManager.createTileUpdaterForApplication();
            tileUpdateManager.enableNotificationQueue(true);
            tileUpdateManager.clear();

            if (posts !== null) {
                
            
                if (posts.length == null) {
                    posts.length = 0;
                }

                for (var i = 0; i < posts.length; i++) {
                    var post = posts[i];
                    var wideTileContent = null;
                
                    if (typeof post.teaserImage !== "undefined" && post.teaserImage !== false) {
                        //create tile with image
                        wideTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileWideSmallImageAndText04();
                        wideTileContent.textHeading.text = post.title,true;
                        wideTileContent.textBodyWrap.text = post.teaserContent,true;
                        wideTileContent.image.src = post.teaserImage;
                        wideTileContent.branding = 2;

                        var squareTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileSquareText04();
                        squareTileContent.textBodyWrap.text = post.title,true;
                        wideTileContent.squareContent = squareTileContent;

                    } else {
                        //create tile without image
                        wideTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileWideBlockAndText02();
                        wideTileContent.textBodyWrap.text = post.title,true;
                        wideTileContent.branding = 2;

                        var squareTileContent = NotificationsExtensions.TileContent.TileContentFactory.createTileSquareText04();
                        squareTileContent.textBodyWrap.text = post.title,true;

                        wideTileContent.squareContent = squareTileContent;
                    }


                    tileUpdateManager.update(wideTileContent.createNotification());
                }
            }
        });
        
    }

    WinJS.Namespace.define("TileUpdate", {
        updateTile: updateTile
    });

})();